using System;
using System.Collections.Generic;
using System.Text;
using Dracham.Network;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Dracham.GUI.Controls;
using Dracham.TileManagement.CharacterManagement;

namespace Dracham.GUI
{
    public class UserInterface
    {
        private DrachamNetwork drachamNetwork;

        #region controls

        public Textbox txtUserInput;
        public Label lblHitpoints;
        public TextViewer txtMessages;
        public Label lblPlayerName;
        public Button btnAttack;

        #endregion

        public UserInterface(DrachamNetwork drachamNetwork, GraphicsDevice gd, ContentManager content)
        {
            this.drachamNetwork = drachamNetwork;
            GUIHelper.Initialize(content);

            txtUserInput = new Textbox("txtUserInput", new Vector2(360, 545 - 17), "couriernew");
            txtMessages = new TextViewer("txtMessages", new Rectangle(264, 390, 342, 110), "couriernew9");
            lblHitpoints = new Label(new Vector2(10, 20), Color.White, content.Load<SpriteFont>("fonts\\couriernew9"));
            lblPlayerName = new Label(new Vector2(10, 10), Color.White, content.Load<SpriteFont>("fonts\\couriernew9"));
            btnAttack = new Button(new Vector2(20, 300), new Rectangle(0, 0, 50, 50), content.Load<Texture2D>("gui\\attack"));

            txtUserInput.ReturnKeyPress += new Textbox.ReturnKeyPressHandler(txtUserInput_ReturnKeyPress);
        }

        private void txtUserInput_ReturnKeyPress()
        {
            Dracham.DrachamNetwork.Invoke(RequestType.ChatMessage, new NetworkParameter(Dracham.SessionKey, 36), new NetworkParameter(Encoding.UTF8.GetBytes(txtUserInput.Text).Length.ToString(), 10), new NetworkParameter(txtUserInput.Text, Encoding.UTF8.GetBytes(txtUserInput.Text).Length));

            if (txtUserInput.Text == "cfwarrior")
            {
                Dracham.Player.Model = ModelFactory.GetModelById("fwarrior");
            }

            txtUserInput.Text = "";
        }

        public void Update(GameTime gameTime)
        {
            Controls.Controls.Update(gameTime);
        }

        public void Render(GameTime gameTime, SpriteBatch batch)
        {
            txtUserInput.Render(gameTime, batch);
            txtMessages.Render(gameTime, batch);
            lblHitpoints.Render(batch, gameTime);
            lblPlayerName.Render(batch, gameTime);
            btnAttack.Render(batch, gameTime);
        }
    }
}